****************************************************************************************************
****************************************************************************************************
*Replication file for:
*The Importance of a Liberal Power's Attention to Democratic Elections Around the World
*Johannes Bubeck Ashrakat Elshehawy Nikolay Marinov Federico Nanni
****************************************************************************************************
****************************************************************************************************

clear
label drop _all
macro drop _all

set more off
drop _all

*please change to your own working directory
cd "/Data"


import delimited month_by_month_results.csv, stringcols(_all)

*	First, generate a date - 1st of the month, and a month var

gen date_str= month+"/"+"1"+"/"+year
gen date=date(date_str,"MDY")
format date %td

drop date_str

*	Ordering

order date month
drop if date==.

destring, replace

gen month_s = mofd(date)
format month_s %tm
rename month month_12
rename month_s month

* This gets a list of prev and foll elections based on nelda data

merge 1:1 ccode year month_12 using nelda_list_staggered.dta

drop if _m==2
drop _m

order date month_12 month stateid nelda_previous nelda_foll types

gsort ccode -month

foreach i of varlist stateid nelda_previous nelda_foll types {

replace `i'=`i'[_n-1] if `i'=="" & `i'[_n-1]!="" & ccode==ccode[_n-1]

}

gsort ccode month


*rename variables
rename n_speeches speeches 
rename n_bills bills 
rename n_laws laws
rename n_papers papers 
rename n_orders orders


*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----
* Figure 2
*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----


*-----
* Figure 2 Part I: A 36 mos before/after election date graph of total speeches, bills, laws (centered at t_0)

capture drop emonth0*

sort ccode date
gen emonth0=0 if year==real(substr(nelda_foll,5,4)) & real(substr(nelda_foll,-7,2))==month_12
replace emonth0=emonth0[_n-1]+1 if emonth0[_n-1]!=.  & emonth0==.

gen emonth0_r=0 if year==real(substr(nelda_foll,5,4)) & real(substr(nelda_foll,-7,2))==month_12
gsort ccode -date
replace emonth0_r=emonth0_r[_n-1]+1 if emonth0_r[_n-1]!=. & emonth0_r==.

sort ccode date

replace emonth0=. if abs(emonth0)>36
replace emonth0_r=. if abs(emonth0_r)>36
replace emonth0=-emonth0_r if emonth0>emonth0_r 
drop emonth0_r
replace emonth0=. if year>2012

sort emonth0

by emonth0: egen n_speeches_sum0=sum(speeches) if emonth0!=.
by emonth0: egen n_laws_sum0=sum(laws) if emonth0!=.
by emonth0: egen n_bills_sum0=sum(bills) if emonth0!=.

egen tag0=tag(emonth0)


*create plot
twoway (line n_laws_sum0 emonth0, lpattern(shortdash_dot) color(blue)) (line n_bills_sum0 emonth0, lpattern(dash) color(red))(line n_speeches_sum0 emonth0,color(green)) if tag0==1, xline(0) graphregion(fcolor(white)) legend(rows(1) order(3 2 1) label(1 "n laws") label(2 "n bills")  label(3 "n speeches") position(6) region(lcolor(black))) xtitle("n months to/from election")

cd "/Figures"
graph export figure2a.pdf, replace


*-----
* Figure 2 Part II: A 36 mos before/after election date graph of total Presidential Orders and Public Papers (centered at t_0)


sort emonth0

by emonth0: egen n_papers_sum0=sum(papers) if emonth0!=.
by emonth0: egen n_orders_sum0=sum(orders) if emonth0!=.

capture drop tag0
egen tag0=tag(emonth0)

*create plot
twoway (line n_orders_sum0 emonth0, lpattern(dash) color(blue)) (line n_papers_sum0 emonth0,color(red)) if tag0==1, xline(0) graphregion(fcolor(white)) legend(rows (1)order(2 1) label(1 "n orders") label(2 "n presidential papers") pos(6) region(lcolor(black))) xtitle("n months to/from election")
cd "/Figures"
graph export figure2b.pdf, replace

*-----
* Appendix figure D.6 - normalized version of figure 2 a

sort ccode date
gen n=1
sort emonth
by emonth0: egen totale=sum(n)


replace n_orders_sum0=n_orders_sum0/totale 
replace n_papers_sum0=n_papers_sum0/totale 
replace n_bills_sum0=n_bills_sum0/totale 
replace n_laws_sum0=n_laws_sum0/totale 
replace n_speeches_sum0=n_speeches_sum0/totale 

gen emonth0_24=emonth0
replace emonth0_24=. if abs(emonth0)>24

*create plot
twoway (line n_laws_sum0 emonth0_24,color(blue)) (line n_bills_sum0 emonth0_24,color(red)) (line n_speeches_sum0 emonth0_24,color(green)) if tag0==1, xline(0) graphregion(fcolor(white))legend(rows(1) order(3 2 1) label(1 "n laws") label(2 "n bills")  label(3 "n speeches") position(6) region(lcolor(black))) xtitle("n months to/from election")
graph export figure_d6.pdf, replace
*-----
* Appendix figure D.7 - normalized version of figure 2b

twoway (line n_bills_sum0 emonth0_24,color(red)) (line n_papers_sum0 emonth0_24,color(blue)) if tag0==1 ,  xline(0) graphregion(fcolor(white))legend(rows(1) order(3 2 1) label(1 "n presidential papers") label(2 "n orders")   position(6) region(lcolor(black))) xtitle("n months to/from election")
cd "/Figures"
graph export figure_d7.pdf, replace


*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----
* Figure 3
*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----

sort ccode date

gen uspreselein3=1  if inrange(date, mdy(11,02,1948)-90, mdy(11,02,1948)+90) & ccode!=2
gen uspreselein6=1  if inrange(date, mdy(11,02,1948)-180, mdy(11,02,1948)+180) & ccode!=2

label var uspreselein3 "US Pres Ele 3 mos"
label var uspreselein6 "US Pres Ele 6 mos"

* A string of dates for the remaining Pres elections 

foreach i in "11,04,1952" "11,06,1956" "11,08,1960" "11,03,1964" "11,05,1968" "11,07,1972" "11,02,1976" "11,04,1980" "11,06,1984" "11,08,1988" "11,03,1992" "11,05,1996" "11,07,2000" "11,02,2004" "11,04,2008" "11,06,2012" { 
replace uspreselein3=1  if inrange(date, mdy(`i')-90, mdy(`i')+90) & ccode!=2
replace uspreselein6=1  if inrange(date, mdy(`i')-180, mdy(`i')+180) & ccode!=2
}

replace uspreselein3=0 if uspreselein3==. & ccode!=2
replace uspreselein6=0 if uspreselein6==. & ccode!=2

* This generates Proximity to US elections - Pres and Midterm ones: vars  uselein3 6
* does the same, starting with Pres, and then using info from a string of midterm elections to add obs

capture drop uselein3 uselein6

gen uselein3=uspreselein3 
gen uselein6=uspreselein6 

label var uselein3 "US Ele 3 mos"
label var uselein6 "US Ele 6 mos"
label var uselein6 "US Ele 6 mos"


foreach i in "11,05,1946" "11,07,1950" "11,02,1954" "11,04,1958" "11,06,1962" "11,08,1966" "11,03,1970" "11,05,1974" "11,07,1978" "11,02,1982" "11,04,1986" "11,06,1990" "11,08,1994" "11,03,1998" "11,05,2002" "11,07,2006" "11,02,2010" { 
replace uselein3=1  if inrange(date, mdy(`i')-90, mdy(`i')+90) & ccode!=2
replace uselein6=1  if inrange(date, mdy(`i')-180, mdy(`i')+180) & ccode!=2
}

replace uselein3=0 if uselein3==. & ccode!=2
replace uselein6=0 if uselein6==. & ccode!=2

label define proximity 0 "Outside US Election Period" 1 "Within US Election Period"
label val uspreselein6 proximity
label val uspreselein3 proximity
label val uselein3  proximity
label val uselein6  proximity


sort ccode date

*-------
*	T-tests and resulting plots
*-------


*-------Figure 3 a


ttest speeches if abs(emonth0)<=36, by(uspreselein3)
input str10 groupstr1 float(mean1 conf_lower1 conf_upper1)
"Outside"  .5976385  .5776745 .6176025
"Within U" .3364108  .3014254 .3713963
end

encode groupstr1, generate(group1)

gen lower_ci1 = conf_lower1
gen upper_ci1 = conf_upper1

twoway  (scatter mean1 group1, mcolor(blue)) ///
        (rcap lower_ci1 upper_ci1 group1, lcolor(blue)) ///
        (line mean1 group1, lpattern(dash) lcolor(black)), ///
        legend(off) ///
        ytitle("") ///
        xtitle("") ///
        xlab(1 "Outside" 2 "Inside", valuelabel) ///
        yscale(range(0.2 0.7)) ///
        ylabel(0.2(0.1)0.7, format(%3.1f))  ///
        yline(0.2, lpattern(solid) lcolor(gs14)) //

		
		cd "/Figures"
graph export figure3a.pdf, replace

	   
*-------Figure 3 b



ttest bills if abs(emonth0)<=36, by(uspreselein3)
input str10 groupstr2 float(mean2 conf_lower2 conf_upper2)
"Outside"  .1848247      .1773683    .1922811
"Within U"  .1615695     .1418718    .1812672
end

encode groupstr2, generate(group2)

gen lower_ci2 = conf_lower2
gen upper_ci2 = conf_upper2

twoway  (scatter mean2 group2, mcolor(blue)) ///
        (rcap lower_ci2 upper_ci2 group2, lcolor(blue)) ///
        (line mean2 group2, lpattern(dash) lcolor(black)), ///
        legend(off) ///
        ytitle("") ///
        xtitle("") ///
        xlab(1 "Outside" 2 "Inside", valuelabel) ///
        yscale(range(0.1 0.2)) ///
        ylabel(0.1(0.02)0.2, format(%3.2f)) ///
        yline(0.2, lpattern(solid) lcolor(gs14)) //
cd "/Figures"
graph export figure3b.pdf, replace



*-------Figure 3 c


ttest papers if abs(emonth0)<=36, by(uspreselein3)
input str10 groupstr3 float(mean3 conf_lower3 conf_upper3)
"Outside"  .0590366       .0555717    .0625015
"Within U"   .0415465         .0343811    .0487119
end

encode groupstr3, generate(group3)

gen lower_ci3 = conf_lower3
gen upper_ci3 = conf_upper3
twoway  (scatter mean3 group3, mcolor(blue)) ///
        (rcap lower_ci3 upper_ci3 group3, lcolor(blue)) ///
        (line mean3 group3, lpattern(dash) lcolor(black)), ///
        legend(off) ///
        ytitle("") ///
        xtitle("") ///
        xlab(1 "Outside" 2 "Inside", valuelabel) ///
        yscale(range(0.02 0.07)) ///
        ylabel(0.02(0.01)0.07, format(%3.2f)) 
cd "/Figures"
graph export figure3c.pdf, replace


*-------Figure 3 d


ttest orders if abs(emonth0)<=36, by(uspreselein3)
input str10 groupstr4 float(mean4 conf_lower4 conf_upper4)
"Outside"   .0018527         .00139    .0023154
"Within U"    .000577    -.000076      .00123
end

encode groupstr4, generate(group4)

gen lower_ci4 = conf_lower4
gen upper_ci4 = conf_upper4
twoway  (scatter mean4 group4, mcolor(blue)) ///
        (rcap lower_ci4 upper_ci4 group4, lcolor(blue)) ///
        (line mean4 group4, lpattern(dash) lcolor(black)), ///
        legend(off) ///
        ytitle("") ///
        xtitle("") ///
        xlab(1 "Outside" 2 "Inside", valuelabel) ///
        yscale(range(0.0 0.004)) ///
        ylabel(0.0(0.001)0.004, format(%4.3f)) 
cd "/Figures"
graph export figure3d.pdf, replace






